using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Data.SqlClient;
using Data;

namespace Security.VisitadorMembership
{
    public class VisitadorMembershipProvider : MembershipProvider
    {
        private String _strName;
        private String _strApplicationName;
        private Boolean _boolEnablePasswordReset;
        private Boolean _boolEnablePasswordRetrieval;
        private Boolean _boolRequiresQuestionAndAnswer;
        private Boolean _boolRequiresUniqueEMail;
        
        private MembershipPasswordFormat _oPasswordFormat;

        string _ConnectionStringName = null;

        public VisitadorMembershipProvider()
        {
            _strName = "";
            _strApplicationName = "";

            _boolRequiresQuestionAndAnswer = false;
            _boolEnablePasswordReset = false;
            _boolEnablePasswordRetrieval = false;
            _boolRequiresQuestionAndAnswer = false;
            _boolRequiresUniqueEMail = false;
        }
        //DFB: reads entries from web.config and initializes this class from those values
        //  Once the provider is loaded, the 
        //  runtime calls Initialize and passes the settings as name-value 
        //  pairs in an instance of the NameValueCollection class.
        //
        public override void Initialize(string strName, System.Collections.Specialized.NameValueCollection config)
        {

            _strName = strName;
            _strApplicationName = "/";
            _ConnectionStringName = config["connectionStringName"];

            _boolRequiresQuestionAndAnswer = false;
            _boolEnablePasswordReset = true;
            _boolEnablePasswordRetrieval = true;
            _boolRequiresQuestionAndAnswer = false;
            _boolRequiresUniqueEMail = true;


        }

        #region No Implementado
        public override string GetPassword(string strName, string strAnswer)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        public override MembershipUser CreateUser(
            string username,
                    string password,
                    string email,
                    string passwordQuestion,
                    string passwordAnswer,
                    bool isApproved,
                    object userId,
                    out MembershipCreateStatus status)
        {

            throw new Exception("The method or operation is not implemented.");
        }
        public override string GetUserNameByEmail(string strEmail)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override string ResetPassword(string strName, string strAnswer)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        public override bool ChangePassword(string strName, string strOldPwd, string strNewPwd)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        public override int GetNumberOfUsersOnline()
        {
            throw new Exception("The method or operation is not implemented.");
        }
        public override bool ChangePasswordQuestionAndAnswer(string strName, string strPassword, string strNewPwdQuestion, string strNewPwdAnswer)
        {
            throw new Exception("The method or operation is not implemented.");

        }
        public override int PasswordAttemptWindow
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }
        public override bool DeleteUser(string strName, bool boolDeleteAllRelatedData)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        public override MembershipUserCollection FindUsersByEmail(string strEmailToMatch, int iPageIndex, int iPageSize, out int iTotalRecords)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        public override MembershipUserCollection FindUsersByName(string strUsernameToMatch, int iPageIndex, int iPageSize, out int iTotalRecords)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        public override MembershipUserCollection GetAllUsers(int iPageIndex, int iPageSize, out int iTotalRecords)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        public override bool UnlockUser(string strUserName)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        public override int MaxInvalidPasswordAttempts
        {
            get { return 1000; }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { return 0; }
        }

        public override int MinRequiredPasswordLength
        {
            get { return 1; }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { return ""; }
        }
        #endregion

        public override bool ValidateUser(string strName, string strPassword)
        {
            Visitante v = new Visitante(new Data.Configuration.Context().GetNewDatabaseConnection());
             v.LoadByName(strName);
             if (v.Simple == null) { return false; }
             bool validated = (String.Equals(v.Simple.Password, strPassword) ? true : false);
             return validated;
        }

        public override string ApplicationName
        {
            get
            {

                return _strApplicationName;
            }
            set
            {
                _strApplicationName = value;
            }
        }
        public override string Name
        {
            get
            {
                return _strName;
            }
        }
        public override bool EnablePasswordReset
        {
            get
            {

                return _boolEnablePasswordReset;
            }
        }
        public override bool EnablePasswordRetrieval
        {
            get
            {
                return _boolEnablePasswordRetrieval;
            }
        }
        public override MembershipPasswordFormat PasswordFormat
        {
            get
            {
                return _oPasswordFormat;
            }
        }
        public override bool RequiresQuestionAndAnswer
        {
            get
            {
                return _boolRequiresQuestionAndAnswer;
            }
        }
        
        public override bool RequiresUniqueEmail
        {
            get
            {
                return _boolRequiresUniqueEMail;
            }
        }

        
        #region GetUser
        public override MembershipUser GetUser(string strName, bool boolUserIsOnline)
        {
            //boolUserIsOnline, la idea es que si llevas un registro de ultimo uso de la cuenta puedas actualizarlo si el parametro viene true
            SimpleVisitante sv = new Visitante(new Data.Configuration.Context().GetNewDatabaseConnection()).LoadByName(strName);
            MembershipUser Out = GetMembershipUserBySimpleUser(sv);
            return Out;
        }
        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            return GetUser((int)providerUserKey, userIsOnline);
        }
        public MembershipUser GetUser(int providerUserKey, bool userIsOnline)
        {

            SimpleVisitante v = new Visitante(new Data.Configuration.Context().GetNewDatabaseConnection()).Load(providerUserKey);
            return GetMembershipUserBySimpleUser(v);
        }

        private MembershipUser GetMembershipUserBySimpleUser(SimpleVisitante v)
        {
            int userId = v.Id;
            string userName = v.UserName;
            MembershipUser Out = new MembershipUser(
                    this.Name
                    , userName
                    , userId
                    , null
                    , null
                    , null
                    , true
                    , true//u.habilitado == 0
                    , new DateTime(1901, 1, 1)
                    , new DateTime(1901, 1, 1)
                    , new DateTime(1901, 1, 1)
                    , new DateTime(1901, 1, 1)
                    , new DateTime(1901, 1, 1));
            return Out;
        }
        #endregion
    }
}